package com.ssm.typehandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import com.alibaba.fastjson.JSON;

@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(com.alibaba.fastjson.JSONObject.class)
public class JSONObjectHandler<JSONObject> extends BaseTypeHandler<JSONObject> {
	 
	  @Override
	  public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType)
	      throws SQLException {
	    ps.setString(i, JSON.toJSONString(parameter));
	  }
	 
	  @Override
	  public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException {
	    return JSON.parseObject(rs.getString(columnName), getRawType());
	  }
	 
	  @Override
	  public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
	    return JSON.parseObject(rs.getString(columnIndex), getRawType());
	  }
	 
	  @Override
	  public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
	    return JSON.parseObject(cs.getString(columnIndex), getRawType());
	  }
	}